<?php


namespace app\admin\member\controller;


use app\admin\member\validate\MemberValidate;
use app\common\controller\BaseController;
use app\model\Member;
use app\model\MemberBalanceLog;
use app\model\MemberIntegralLog;
use app\model\Order;
use support\utils\Time;

class MemberController extends BaseController
{
    protected $model = Member::class;

    public function list()
    {
        $form = request()->getForm();

        $where = [];
        $form['username'] && $where[] = ['username', 'like', "%" . $form['username'] . "%"];
        $form['nickname'] && $where[] = ['nickname', 'like', "%" . $form['nickname'] . "%"];
        $form['mobile'] && $where[] = ['mobile', 'like', "%" . $form['mobile'] . "%"];
        if ($form['created_start'] && $form['created_end']) {
            $where[] = ['created_at', 'between', [$form['created_start'], $form['created_end']]];
        }
        $count = $this->getModel()
            ->where($where)
            ->count();

        $data = $this->getModel()
            ->with([
                'level'
            ])
            ->where($where)
            ->limit($form->getOffset(), $form->getPageSize())
            ->select();
        return $this->success($this->formatPage($count, $data));
    }

    public function add()
    {
        $data = request()->all();
        MemberValidate::goCheck($data, 'add');
        $this->getModel()->insert($data);
        return $this->success();
    }

    public function update()
    {
        $data = request()->all();
        MemberValidate::goCheck($data, 'update');
        $this->getModel()->where('id', '=', $data['id'])->update($data);
        return $this->success();
    }

    public function getById()
    {
        $id = input('id');
        MemberValidate::goCheck(input(), 'id');
        $data = $this->getModel()->with([
            'level'
        ])->where('id', '=', $id)->find();
        return $this->success($data);
    }

    public function deleteById()
    {
        $id = input('id');
        MemberValidate::goCheck(input(), 'id');
        $this->getModel()->where('id', '=', $id)->delete();
        return $this->success();
    }


    /**
     * 积分记录
     */
    public function integralRecord()
    {
        $form = request()->getForm();
        $id = input('id');
        MemberValidate::goCheck(input(), 'id');
        $where = [
            ['member_id', '=', $id]
        ];
        $count = MemberIntegralLog::where($where)
            ->count();

        $data = MemberIntegralLog::where($where)
            ->order("created_at desc")
            ->limit($form->getOffset(), $form->getPageSize())
            ->select();

        return $this->success($this->formatPage($count, $data));
    }

    /**
     * 订单记录
     */
    public function orderRecord()
    {
        $form = request()->getForm();
        $id = input('id');
        MemberValidate::goCheck(input(), 'id');
        $where = [
            ['member_id', '=', $id]
        ];
        $count = Order::where($where)
            ->count();

        $data = Order::where($where)
            ->limit($form->getOffset(), $form->getPageSize())
            ->select();
        return $this->success($this->formatPage($count, $data));
    }

    /**
     * 余额记录
     */
    public function balanceRecord()
    {
        $form = request()->getForm();
        $id = input('id');
        MemberValidate::goCheck(input(), 'id');
        $where = [
            ['member_id', '=', $id]
        ];
        $count = MemberBalanceLog::where($where)
            ->count();

        $data = MemberBalanceLog::where($where)
            ->limit($form->getOffset(), $form->getPageSize())
            ->select();
        return $this->success($this->formatPage($count, $data));
    }

}